<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CI_Controller {

    public function __construct() {
      parent::__construct();
      
       $this->load->helper('language');
      // Set language      
      $this->session->set_userdata('lang','de');
    
      // Load language file
      $this->lang->load($this->session->userdata('lang'));

      // Load library modules
      $this->load->library('modules');
      
      $this->load->model('users_mod');
      $this->load->model('cities_mod');
      $this->load->model('images_mod');
    }
	/**
	 *
	 */
	public function index()
	{       
        $data['title'] = lang('search_title');
        $data['keywords'] = lang('search_keywords');
        $data['description'] = lang('search_description');
        $data['cities'] = $this->cities_mod->getCitiesList();
        $filter = $this->input->post('filter');
        $data['filter'] = $filter;
        //print_r($filter);
        if( count($filter)>0 && (isset($filter['postal_code']) || isset($filter['name']) ) )
        {          
//          $year = $filter['year'];
//          $filter['year_from'] = @substr($year, 0, 2);
//          $filter['year_to'] = @substr($year, 5, 2);
          $data['users'] = $this->users_mod->getSearchUsers($filter);
          
        }
        //$data['']
        $data['menu_active'] = "search";
        $data['contents']  = "search/home";   
		$this->load->view('template/main',$data);
	}
    
    /**
     *  Ajax search result
     * 
     */
    public function getSearchResult()
    {
        try{
          $filter = $this->input->post('filter');
 
          $year = $filter['year'];
          $filter['year_from'] = substr($year, 0, 2);
          $filter['year_to'] = substr($year, 5, 2);
          //print_r($filter);
          if( NULL != ($users = $this->users_mod->getSearchUsers($filter)) )
          {
            //print_r($users);
//            $full_users = array();
//            foreach ($users as $user)
//            {
//              $user->image_name = $this->images_mod->getMainImage($user->id);
//              $full_users[] = $user;
//            }
            //print_r($full_users);
            //$data['users'] = $full_users;
            $data['users'] = $users;
            $html = $this->load->view('search/search_results',$data,true);
            $response = array(
                          'ind' => 1,
                          'msg' => "",
                          'html' => $html
                      );
          }          
          else
          {
            $response = array(
                        'ind' => 0,
                        'msg' => lang('users_not_found')
                    );
          }
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
} // end class
/* End of file search.php */
/* Location: ./application/controllers/search.php */